{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1.import libs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\"\"A script for data collection on Quafu platform.\"\"\"\n",
    "import sys\n",
    "import os\n",
    "sys.path.append(os.path.abspath(os.path.join(os.getcwd(), '..')))\n",
    "\n",
    "from utils.get_data import ShotTestReceiver, CircuitReceiver\n",
    "from qiskit import QuantumCircuit, ClassicalRegister\n",
    "from qiskit.qasm2 import dumps, loads  # Use this for QASM conversion\n",
    "import itertools\n",
    "import random"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2.define some helpful functions\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def create_test_circuit(qubit_indices: list, circuit_type: str, mapping: dict = None,\n",
    "                         qed_pilocy: str='0011', xtest_gate_num: int=0, number_of_gates: int=0,\n",
    "                         measure_all: bool=True) -> str:\n",
    "    \n",
    "    \"\"\"Create a test circuit with Hadamard gates and CNOT gates.\n",
    "    \n",
    "    This function creates a quantum circuit that applies Hadamard gates and CNOT gates\n",
    "    to specified physical qubits and converts it to QASM format.\n",
    "    \n",
    "    Args:\n",
    "        qubit_indices: List of physical qubit indices to map the logical qubits to\n",
    "        circuit_type: str, 'H' or 'random_1'\n",
    "        mapping: dict, the mapping of the qubits to the logical qubits, e.g. {37: [44]}, means the state of qubit 37 is copyed to the qubit 44.\n",
    "        xtest_gate_num: int, number of X test gates to apply to mapping qubit\n",
    "        qed_pilocy: str, the qed pilocy to use, e.g. '0011', '0110'\n",
    "        number_of_gates: int, number of gates to apply to each qubit\n",
    "\n",
    "    Returns:\n",
    "        str: QASM representation of the circuit\n",
    "    \"\"\"\n",
    "    import math\n",
    "\n",
    "    # Create circuit with enough qubits for the physical indices\n",
    "    max_qubit_idx = max(qubit_indices) + 1\n",
    "    circuit = QuantumCircuit(max_qubit_idx, len(qubit_indices))\n",
    "\n",
    "    if circuit_type == 'None':\n",
    "        if mapping:\n",
    "            for qubit, map_qubits in mapping.items():\n",
    "                for map_qubit in map_qubits:\n",
    "                    circuit.cx(qubit, map_qubit)\n",
    "                    if qed_pilocy == '0110':\n",
    "                        circuit.x(map_qubit)\n",
    "                    \n",
    "                        for _ in range(xtest_gate_num):\n",
    "                            circuit.x(map_qubit)\n",
    "\n",
    "    elif circuit_type == 'H':\n",
    "        # Add hadamard gates using physical qubit indices directly\n",
    "        circuit.h(qubit_indices[0])  # Single Hadamard gate\n",
    "        if mapping:\n",
    "            for qubit, map_qubits in mapping.items():\n",
    "                for map_qubit in map_qubits:\n",
    "                    circuit.cx(qubit, map_qubit)\n",
    "                    if qed_pilocy == '0110':\n",
    "                        circuit.x(map_qubit)\n",
    "                    \n",
    "                        for _ in range(xtest_gate_num):\n",
    "                            circuit.x(map_qubit)\n",
    "            \n",
    "    elif circuit_type == 'random':\n",
    "        # Add 1-qubit gates randomly\n",
    "        # Add random single-qubit gates\n",
    "        for qubit, map_qubits in mapping.items():            \n",
    "            # Choose a random number of gates to apply to this qubit (1-3)\n",
    "            num_gates_per_qubit = number_of_gates\n",
    "            \n",
    "            for _ in range(num_gates_per_qubit):\n",
    "                # Select a random gate from the single-qubit gate set\n",
    "                gate_type = random.choice(['h', 'x', 'y', 'z', 'rx', 'ry', 'rz'])\n",
    "                \n",
    "                # Apply the selected gate\n",
    "                if gate_type == 'h':\n",
    "                    circuit.h(qubit)\n",
    "                elif gate_type == 'x':\n",
    "                    circuit.x(qubit)\n",
    "                elif gate_type == 'y':\n",
    "                    circuit.y(qubit)\n",
    "                elif gate_type == 'z':\n",
    "                    circuit.z(qubit)\n",
    "                elif gate_type == 'rx':\n",
    "                    # Random angle between 0 and 2π using math.pi for accuracy\n",
    "                    theta = random.uniform(-1*math.pi+1e-5, math.pi-1e-5)\n",
    "                    circuit.rx(theta, qubit)\n",
    "                elif gate_type == 'ry':\n",
    "                    theta = random.uniform(-1*math.pi+1e-5, math.pi-1e-5)\n",
    "                    circuit.ry(theta, qubit)\n",
    "                elif gate_type == 'rz':\n",
    "                    theta = random.uniform(-1*math.pi+1e-5, math.pi-1e-5)\n",
    "                    circuit.rz(theta, qubit)\n",
    "\n",
    "            for map_qubit in map_qubits:\n",
    "                circuit.cx(qubit, map_qubit)\n",
    "                if qed_pilocy == '0110':\n",
    "                    circuit.x(map_qubit)\n",
    "                    for _ in range(xtest_gate_num):\n",
    "                        circuit.x(map_qubit)\n",
    "        if mapping is None:\n",
    "            qubit = qubit_indices[0]\n",
    "            # Choose a random number of gates to apply to this qubit (1-3)\n",
    "            num_gates_per_qubit = number_of_gates\n",
    "            \n",
    "            for _ in range(num_gates_per_qubit):\n",
    "                # Select a random gate from the single-qubit gate set\n",
    "                gate_type = random.choice(['h', 'x', 'y', 'z', 'rx', 'ry', 'rz'])\n",
    "                \n",
    "                # Apply the selected gate\n",
    "                if gate_type == 'h':\n",
    "                    circuit.h(qubit)\n",
    "                elif gate_type == 'x':\n",
    "                    circuit.x(qubit)\n",
    "                elif gate_type == 'y':\n",
    "                    circuit.y(qubit)\n",
    "                elif gate_type == 'z':\n",
    "                    circuit.z(qubit)\n",
    "                elif gate_type == 'rx':\n",
    "                    # Random angle between 0 and 2π using math.pi for accuracy\n",
    "                    theta = random.uniform(-1*math.pi+1e-5, math.pi-1e-5)\n",
    "                    circuit.rx(theta, qubit)\n",
    "                elif gate_type == 'ry':\n",
    "                    theta = random.uniform(-1*math.pi+1e-5, math.pi-1e-5)\n",
    "                    circuit.ry(theta, qubit)\n",
    "                elif gate_type == 'rz':\n",
    "                    theta = random.uniform(-1*math.pi+1e-5, math.pi-1e-5)\n",
    "                    circuit.rz(theta, qubit)\n",
    "            \n",
    "\n",
    "\n",
    "    # Add measurements for all qubits\n",
    "    if measure_all:\n",
    "        for i, qubit in enumerate(qubit_indices):\n",
    "            circuit.measure(qubit, i)\n",
    "    \n",
    "    return dumps(circuit)\n",
    "\n",
    "def create_readout_benchmark_circuit(qubit_indices: list, use_QED: bool=False, use_QED_reverse: bool=False, QED_map: dict=None, inherit_circuit: str=None) -> list[str]:\n",
    "    \"\"\"Create benchmark circuits for readout error characterization.\n",
    "    \n",
    "    This function generates circuits that prepare all possible computational basis\n",
    "    states for the specified qubits to characterize readout errors.\n",
    "    \n",
    "    Args:\n",
    "        qubit_indices: List of physical qubit indices to benchmark\n",
    "        use_QED: bool, whether to use QED to encode the state\n",
    "        use_QED_reverse: bool, whether to use reverse QED to encode the state\n",
    "        QED_map: dict, the mapping of the qubits to the QED qubits, e.g. {37: [44]}, means the qubit 37 is mapped to the qubit 44 in the QED\n",
    "        \n",
    "    Returns:\n",
    "        list[str]: List of QASM strings for all state preparation combinations\n",
    "    \"\"\"\n",
    "    n_qubits = len(qubit_indices)\n",
    "    benchmark_n = len(qubit_indices)\n",
    "    if QED_map is not None:\n",
    "        benchmark_n = len(QED_map)\n",
    "    preparations = list(itertools.product([0,1], repeat=benchmark_n))\n",
    "    circuits = []\n",
    "    \n",
    "    for prep in preparations:\n",
    "        max_qubit_idx = max(qubit_indices) + 1\n",
    "        if inherit_circuit is not None:\n",
    "            circuit = loads(inherit_circuit)\n",
    "        else:\n",
    "            circuit = QuantumCircuit(max_qubit_idx, n_qubits)\n",
    "        \n",
    "        # Prepare basis states\n",
    "        for qubit, state in zip(qubit_indices, prep):\n",
    "            if state == 1:\n",
    "                circuit.x(qubit)\n",
    "\n",
    "        if use_QED:\n",
    "            for qubit, map_qubits in QED_map.items():\n",
    "                for map_qubit in map_qubits:\n",
    "                    circuit.cx(qubit, map_qubit)\n",
    "        if use_QED_reverse:\n",
    "            for qubit, map_qubits in QED_map.items():\n",
    "                for map_qubit in map_qubits:\n",
    "                    circuit.cx(qubit, map_qubit)\n",
    "                    circuit.x(map_qubit)\n",
    "\n",
    "        # Add measurements\n",
    "        for i, qubit in enumerate(qubit_indices):\n",
    "            circuit.measure(qubit, i)\n",
    "            \n",
    "        circuits.append(dumps(circuit))\n",
    "        \n",
    "    return circuits\n",
    "\n",
    "\n",
    "def create_repetition_code_circuit(qubit_indices: list, circuit_type: str, number_of_gates: int=0, targer_qubit_indices: list=[]) -> str:\n",
    "    \"\"\"Create a n-qubit repetition code circuit for different circuit types.\n",
    "    \n",
    "    This function implements a simple 3-qubit repetition code with\n",
    "    a Hadamard gate on the first qubit followed by encoding operations.\n",
    "    \n",
    "    Args:\n",
    "        qubit_indices: List of n physical qubit indices\n",
    "        circuit_type: str, 'hadamard_1' or 'random_1'\n",
    "        number_of_gates: int, number of gates to apply to each qubit\n",
    "        targer_qubit_indices: list, list of qubit indices to apply gates to\n",
    "        \n",
    "    Returns:\n",
    "        str: QASM representation of the repetition code circuit\n",
    "    \"\"\"\n",
    "    if len(qubit_indices) < 3:\n",
    "        raise ValueError(\"Need at least 3 physical qubits for repetition code\")\n",
    "        \n",
    "    max_qubit_idx = max(qubit_indices) + 1\n",
    "    circuit = QuantumCircuit(max_qubit_idx, len(qubit_indices))\n",
    "    \n",
    "    # Initial Hadamard and encoding\n",
    "    if circuit_type == 'hadamard_1':\n",
    "        circuit.h(qubit_indices[0])\n",
    "    elif circuit_type == 'random_1':\n",
    "        import math\n",
    "        for qubit in targer_qubit_indices:\n",
    "            # Choose a random number of gates to apply to this qubit (1-3)\n",
    "            num_gates_per_qubit = number_of_gates\n",
    "            \n",
    "            for _ in range(num_gates_per_qubit):\n",
    "                # Select a random gate from the single-qubit gate set\n",
    "                gate_type = random.choice(['h', 'x', 'y', 'z', 'rx', 'ry', 'rz'])\n",
    "                \n",
    "                # Apply the selected gate\n",
    "                if gate_type == 'h':\n",
    "                    circuit.h(qubit)\n",
    "                elif gate_type == 'x':\n",
    "                    circuit.x(qubit)\n",
    "                elif gate_type == 'y':\n",
    "                    circuit.y(qubit)\n",
    "                elif gate_type == 'z':\n",
    "                    circuit.z(qubit)\n",
    "                elif gate_type == 'rx':\n",
    "                    # Random angle between 0 and 2π using math.pi for accuracy\n",
    "                    theta = random.uniform(0, 2 * math.pi)\n",
    "                    circuit.rx(theta, qubit)\n",
    "                elif gate_type == 'ry':\n",
    "                    theta = random.uniform(0, 2 * math.pi)\n",
    "                    circuit.ry(theta, qubit)\n",
    "                elif gate_type == 'rz':\n",
    "                    theta = random.uniform(0, 2 * math.pi)\n",
    "                    circuit.rz(theta, qubit)\n",
    "\n",
    "    # encode\n",
    "    for i in range(len(qubit_indices)-1):\n",
    "        circuit.cx(qubit_indices[i], qubit_indices[i+1])\n",
    "\n",
    "    # Measurements\n",
    "    for i, qubit in enumerate(qubit_indices):\n",
    "        circuit.measure(qubit, i)\n",
    "        \n",
    "    return dumps(circuit)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 3.get data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Execution date: 2025-04-09\n"
     ]
    }
   ],
   "source": [
    "# Get the current date when this code is executed\n",
    "from datetime import datetime\n",
    "date = datetime.now().strftime(\"%Y-%m-%d\")\n",
    "print(f\"Execution date: {date}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 3.1 receive data of shot test  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "qubit_indices = [44, 50]    # readout error rate 1.38 1.14\n",
    "# qubit_indices = [20, 26]    # readout error rate 1.12 1.26\n",
    "# qubit_indices = [2, 9]    # readout error rate 1.67 1.6\n",
    "# qubit_indices = [23, 29] # readout error rate 2.23 2.39\n",
    "\n",
    "circuit_type = 'H'\n",
    "number_of_gates = 10\n",
    "shot_range = [5*int(1e3), 7*int(1e3), int(1e4)] #, int(1e5)]\n",
    "# login_key = 'kCO7NfSBK79mgrEV+zE39oeGLBVrY+JUBQ07lp22NL0='\n",
    "login_key = 'gtKfDJ1O/qUncccbTQ6m1wYyNygTb/NW/T+y8DAAwvI='\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Job submission completed.\n",
      "Job submission completed.\n",
      "Job submission completed.\n"
     ]
    }
   ],
   "source": [
    "qed_pilocy = '01'\n",
    "xtest_gate_num = 0\n",
    "\n",
    "shot_test_platform_config = {\n",
    "    'platform': 'tianyan',\n",
    "    'login_key': login_key,\n",
    "    # 'machine_name': 'tianyan_tnn',\n",
    "    'machine_name': 'tianyan176-2',\n",
    "    'num_qubits': len(qubit_indices),\n",
    "    'exp_name': f'{len(qubit_indices)}-qubit_{circuit_type}_{qed_pilocy}_qubits_{\"-\".join(map(str, qubit_indices))}'  # Include number of qubits, circuit type and qubit indices\n",
    "}\n",
    "\n",
    "\n",
    "test_circuits_receiver = ShotTestReceiver(shot_test_platform_config, shot_range=shot_range, circuit_nums=1)\n",
    "test_circuits_receiver.submit_experiment(create_test_circuit(qubit_indices=[qubit_indices[0]], circuit_type=circuit_type,\n",
    "                                          mapping=None, qed_pilocy=qed_pilocy, xtest_gate_num=xtest_gate_num, \n",
    "                                          number_of_gates=number_of_gates))\n",
    "test_circuits_receiver.receive_results()\n",
    "test_circuits_receiver.save_results(f'../results/{date}/test_circuits/')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Job submission completed.\n",
      "Job submission completed.\n",
      "Job submission completed.\n"
     ]
    }
   ],
   "source": [
    "qed_pilocy = '0011'\n",
    "\n",
    "shot_test_platform_config = {\n",
    "    'platform': 'tianyan',\n",
    "    'login_key': login_key,\n",
    "    # 'machine_name': 'tianyan_tnn',\n",
    "    'machine_name': 'tianyan176-2',\n",
    "    'num_qubits': len(qubit_indices),\n",
    "    'exp_name': f'{len(qubit_indices)}-qubit_{circuit_type}_{qed_pilocy}_qubits_{\"-\".join(map(str, qubit_indices))}'  # Include number of qubits, circuit type and qubit indices\n",
    "}\n",
    "\n",
    "\n",
    "test_circuits_receiver = ShotTestReceiver(shot_test_platform_config, shot_range=shot_range, circuit_nums=1)\n",
    "test_circuits_receiver.submit_experiment(create_test_circuit(qubit_indices=qubit_indices, circuit_type=circuit_type,\n",
    "                                          mapping={qubit_indices[0]: [qubit_indices[1]]}, qed_pilocy=qed_pilocy, xtest_gate_num=xtest_gate_num, \n",
    "                                          number_of_gates=number_of_gates))\n",
    "test_circuits_receiver.receive_results()\n",
    "test_circuits_receiver.save_results(f'../results/{date}/test_circuits/')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "WARNING:root:Results not ready yet: 'Failed to query the experimental result.'. Please try again later.\n"
     ]
    },
    {
     "ename": "KeyboardInterrupt",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m                         Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[7], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[43mtest_circuits_receiver\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mreceive_results\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m      2\u001b[0m test_circuits_receiver\u001b[38;5;241m.\u001b[39msave_results(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m../results/\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mdate\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m/test_circuits/\u001b[39m\u001b[38;5;124m'\u001b[39m)\n",
      "File \u001b[1;32md:\\code_space\\110meeting\\day1 get data from quantum cloud platform\\utils\\get_data.py:282\u001b[0m, in \u001b[0;36mShotTestReceiver.receive_results\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m    280\u001b[0m \u001b[38;5;250m\u001b[39m\u001b[38;5;124;03m\"\"\"Collect results from all experiments.\"\"\"\u001b[39;00m\n\u001b[0;32m    281\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m data_receiver \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mdata_receivers:\n\u001b[1;32m--> 282\u001b[0m     \u001b[43mdata_receiver\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mreceive_results\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n",
      "File \u001b[1;32md:\\code_space\\110meeting\\day1 get data from quantum cloud platform\\utils\\get_data.py:189\u001b[0m, in \u001b[0;36mGetData.receive_results\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m    186\u001b[0m logging\u001b[38;5;241m.\u001b[39minfo(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mAttempting to retrieve experiment results\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m    188\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m--> 189\u001b[0m     \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_data\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mreceive_result\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m    190\u001b[0m     \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_cleanup_temp_file()\n\u001b[0;32m    191\u001b[0m     logging\u001b[38;5;241m.\u001b[39minfo(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mSuccessfully retrieved experiment results\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n",
      "File \u001b[1;32md:\\code_space\\110meeting\\day1 get data from quantum cloud platform\\utils\\cloud\\tianyan_data.py:93\u001b[0m, in \u001b[0;36mTianyanData.receive_result\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m     91\u001b[0m \u001b[38;5;250m\u001b[39m\u001b[38;5;124;03m\"\"\"Collect experiment results from platform.\"\"\"\u001b[39;00m\n\u001b[0;32m     92\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mnum_qubits \u001b[38;5;241m<\u001b[39m \u001b[38;5;241m15\u001b[39m:\n\u001b[1;32m---> 93\u001b[0m     \u001b[43mget_result_less_15qubits\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mdf\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mplatform\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m     94\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m     95\u001b[0m     get_result_more_15qubits(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mdf, \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mplatform)\n",
      "File \u001b[1;32md:\\code_space\\110meeting\\day1 get data from quantum cloud platform\\utils\\cloud\\tianyan_platform.py:62\u001b[0m, in \u001b[0;36mget_result_less_15qubits\u001b[1;34m(df, platform)\u001b[0m\n\u001b[0;32m     59\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124m[\u001b[39m\u001b[38;5;124m'\u001b[39m \u001b[38;5;129;01min\u001b[39;00m exp_id:\n\u001b[0;32m     60\u001b[0m     exp_id \u001b[38;5;241m=\u001b[39m \u001b[38;5;28meval\u001b[39m(exp_id)[\u001b[38;5;241m0\u001b[39m]\n\u001b[1;32m---> 62\u001b[0m result \u001b[38;5;241m=\u001b[39m \u001b[43mplatform\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mquery_experiment\u001b[49m\u001b[43m(\u001b[49m\n\u001b[0;32m     63\u001b[0m \u001b[43m    \u001b[49m\u001b[43mquery_id\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mexp_id\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m     64\u001b[0m \u001b[43m    \u001b[49m\u001b[43mmax_wait_time\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;241;43m120\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[0;32m     65\u001b[0m \u001b[43m    \u001b[49m\u001b[43msleep_time\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;241;43m5\u001b[39;49m\n\u001b[0;32m     66\u001b[0m \u001b[43m\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m     68\u001b[0m df\u001b[38;5;241m.\u001b[39mloc[row, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mexp_result\u001b[39m\u001b[38;5;124m'\u001b[39m] \u001b[38;5;241m=\u001b[39m json\u001b[38;5;241m.\u001b[39mdumps(\n\u001b[0;32m     69\u001b[0m     json\u001b[38;5;241m.\u001b[39mloads(result[\u001b[38;5;241m0\u001b[39m][\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mprobability\u001b[39m\u001b[38;5;124m'\u001b[39m])\n\u001b[0;32m     70\u001b[0m )\n\u001b[0;32m     72\u001b[0m \u001b[38;5;66;03m# Apply calibration and correction\u001b[39;00m\n",
      "File \u001b[1;32md:\\code_space\\110meeting\\day1 get data from quantum cloud platform\\.venv\\lib\\site-packages\\cqlib\\quantum_platform\\base.py:429\u001b[0m, in \u001b[0;36mBasePlatform.query_experiment\u001b[1;34m(self, query_id, max_wait_time, sleep_time)\u001b[0m\n\u001b[0;32m    427\u001b[0m         logger\u001b[38;5;241m.\u001b[39merror(e)\n\u001b[0;32m    428\u001b[0m     logger\u001b[38;5;241m.\u001b[39minfo(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mwaiting for \u001b[39m\u001b[38;5;132;01m%d\u001b[39;00m\u001b[38;5;124m seconds\u001b[39m\u001b[38;5;124m'\u001b[39m, sleep_time)\n\u001b[1;32m--> 429\u001b[0m     \u001b[43mtime\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43msleep\u001b[49m\u001b[43m(\u001b[49m\u001b[43msleep_time\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m    430\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m CqlibRequestError(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mFailed to query the experimental result.\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m: "
     ]
    }
   ],
   "source": [
    "test_circuits_receiver.receive_results()\n",
    "test_circuits_receiver.save_results(f'../results/{date}/test_circuits/')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "qed_pilocy = '0110'\n",
    "\n",
    "shot_test_platform_config = {\n",
    "    'platform': 'tianyan',\n",
    "    'login_key': login_key,\n",
    "    # 'machine_name': 'tianyan_tnn',\n",
    "    'machine_name': 'tianyan176-2',\n",
    "    'num_qubits': len(qubit_indices),\n",
    "    'exp_name': f'{len(qubit_indices)}-qubit_{circuit_type}_{qed_pilocy}_qubits_{\"-\".join(map(str, qubit_indices))}'  # Include number of qubits, circuit type and qubit indices\n",
    "}\n",
    "\n",
    "\n",
    "test_circuits_receiver = ShotTestReceiver(shot_test_platform_config, shot_range=shot_range, circuit_nums=1)\n",
    "test_circuits_receiver.submit_experiment(create_test_circuit(qubit_indices=qubit_indices, circuit_type=circuit_type,\n",
    "                                          mapping={qubit_indices[0]: [qubit_indices[1]]}, qed_pilocy=qed_pilocy, xtest_gate_num=xtest_gate_num, \n",
    "                                          number_of_gates=number_of_gates))\n",
    "test_circuits_receiver.receive_results()\n",
    "test_circuits_receiver.save_results(f'../results/{date}/test_circuits/')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_circuits_receiver.receive_results()\n",
    "test_circuits_receiver.save_results(f'../results/{date}/test_circuits/')\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 3.2.receive data of benchmark test"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.2.1 1-qubit benchmark"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### original benchmark"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "for qubit in qubit_indices:\n",
    "    single_qubit_indices = [qubit]\n",
    "    benchmark_type = 'benchmark'\n",
    "    n_qubits = len(single_qubit_indices)\n",
    "    shot_test_platform_config = {\n",
    "        'platform': 'tianyan',\n",
    "        # 'login_key': '8N8TOqEEhrGznQPEoguaiDF6NEFWjSdL2UlZwDV9Ks8.Qf3YjMyQTNyQzNxojIwhXZiwiM4kDN6ICZpJye.9JiN1IzUIJiOicGbhJCLiQ1VKJiOiAXe0Jye',\n",
    "        'login_key': 'kCO7NfSBK79mgrEV+zE39oeGLBVrY+JUBQ07lp22NL0=',\n",
    "        'machine_name': 'tianyan176-2',\n",
    "        'num_qubits': n_qubits,\n",
    "        'exp_name': f'{n_qubits}-qubit_qubits_{\"-\".join(map(str, single_qubit_indices))}'  # Include number of qubits, circuit type and qubit indices\n",
    "    }\n",
    "\n",
    "    # shot_range = [int(1e3), 5*int(1e3), int(1e4)] #, int(1e5)]    # different shots\n",
    "    shot_range = [int(1e4)]     # same shots\n",
    "    benchmark_receiver = ShotTestReceiver(shot_test_platform_config, shot_range=shot_range, circuit_nums=1)\n",
    "\n",
    "    benchmark_receiver.submit_experiment(create_readout_benchmark_circuit(qubit_indices=single_qubit_indices))\n",
    "\n",
    "    benchmark_receiver.receive_results()\n",
    "    benchmark_receiver.save_results(f'../results/{date}/{benchmark_type}/')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.2.2 2-qubit benchmark"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### original benchmark"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "benchmark_type = 'benchmark'\n",
    "n_qubits = len(qubit_indices)\n",
    "shot_test_platform_config = {\n",
    "    'platform': 'tianyan',\n",
    "    # 'login_key': '8N8TOqEEhrGznQPEoguaiDF6NEFWjSdL2UlZwDV9Ks8.Qf3YjMyQTNyQzNxojIwhXZiwiM4kDN6ICZpJye.9JiN1IzUIJiOicGbhJCLiQ1VKJiOiAXe0Jye',\n",
    "    'login_key': 'kCO7NfSBK79mgrEV+zE39oeGLBVrY+JUBQ07lp22NL0=',\n",
    "    'machine_name': 'tianyan176-2',\n",
    "    'num_qubits': n_qubits,\n",
    "    'exp_name': f'{n_qubits}-qubit_qubits_{\"-\".join(map(str, qubit_indices))}'  # Include number of qubits, circuit type and qubit indices\n",
    "}\n",
    "\n",
    "# shot_range = [int(1e3), 5*int(1e3), int(1e4)] #, int(1e5)]    # different shots\n",
    "shot_range = [int(1e4)]     # same shots\n",
    "benchmark_receiver = ShotTestReceiver(shot_test_platform_config, shot_range=shot_range, circuit_nums=1)\n",
    "\n",
    "benchmark_receiver.submit_experiment(create_readout_benchmark_circuit(qubit_indices=qubit_indices))\n",
    "\n",
    "benchmark_receiver.receive_results()\n",
    "benchmark_receiver.save_results(f'../results/{date}/{benchmark_type}/')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### post benchmark (for 0011)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "benchmark_type = 'benchmark'\n",
    "n_qubits = len(qubit_indices)\n",
    "shot_test_platform_config = {\n",
    "    'platform': 'tianyan',\n",
    "    # 'login_key': '8N8TOqEEhrGznQPEoguaiDF6NEFWjSdL2UlZwDV9Ks8.Qf3YjMyQTNyQzNxojIwhXZiwiM4kDN6ICZpJye.9JiN1IzUIJiOicGbhJCLiQ1VKJiOiAXe0Jye',\n",
    "    'login_key': 'kCO7NfSBK79mgrEV+zE39oeGLBVrY+JUBQ07lp22NL0=',\n",
    "    'machine_name': 'tianyan176-2',\n",
    "    'num_qubits': n_qubits,\n",
    "    'exp_name': f'{n_qubits}-qubit_qubits_{\"-\".join(map(str, qubit_indices))}'  # Include number of qubits, circuit type and qubit indices\n",
    "}\n",
    "\n",
    "# shot_range = [int(1e3), 5*int(1e3), int(1e4)] #, int(1e5)]    # different shots\n",
    "shot_range = [int(1e4)]     # same shots\n",
    "benchmark_receiver = ShotTestReceiver(shot_test_platform_config, shot_range=shot_range, circuit_nums=1)\n",
    "\n",
    "benchmark_receiver.submit_experiment(create_readout_benchmark_circuit(qubit_indices=qubit_indices), use_QED=True, QED_map={qubit_indices[0]:[qubit_indices[1]]})\n",
    "\n",
    "benchmark_receiver.receive_results()\n",
    "benchmark_receiver.save_results(f'../results/{date}/{benchmark_type}/')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### post benchmark (for 0110)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "benchmark_type = 'benchmark'\n",
    "n_qubits = len(qubit_indices)\n",
    "shot_test_platform_config = {\n",
    "    'platform': 'tianyan',\n",
    "    # 'login_key': '8N8TOqEEhrGznQPEoguaiDF6NEFWjSdL2UlZwDV9Ks8.Qf3YjMyQTNyQzNxojIwhXZiwiM4kDN6ICZpJye.9JiN1IzUIJiOicGbhJCLiQ1VKJiOiAXe0Jye',\n",
    "    'login_key': 'kCO7NfSBK79mgrEV+zE39oeGLBVrY+JUBQ07lp22NL0=',\n",
    "    'machine_name': 'tianyan176-2',\n",
    "    'num_qubits': n_qubits,\n",
    "    'exp_name': f'{n_qubits}-qubit_qubits_{\"-\".join(map(str, qubit_indices))}'  # Include number of qubits, circuit type and qubit indices\n",
    "}\n",
    "\n",
    "# shot_range = [int(1e3), 5*int(1e3), int(1e4)] #, int(1e5)]    # different shots\n",
    "shot_range = [int(1e4)]     # same shots\n",
    "benchmark_receiver = ShotTestReceiver(shot_test_platform_config, shot_range=shot_range, circuit_nums=1)\n",
    "\n",
    "benchmark_receiver.submit_experiment(create_readout_benchmark_circuit(qubit_indices=qubit_indices), use_QED=True, QED_map={qubit_indices[0]:[qubit_indices[1]]})\n",
    "\n",
    "benchmark_receiver.receive_results()\n",
    "benchmark_receiver.save_results(f'../results/{date}/{benchmark_type}/')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 3.3 receive data of reptition code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "# qubit_indices = [56, 57, 58]\n",
    "# circuit_type = 'hadamard_1'\n",
    "\n",
    "# shot_test_platform_config = {\n",
    "#     'platform': 'quafu',\n",
    "#     'login_key': '8N8TOqEEhrGznQPEoguaiDF6NEFWjSdL2UlZwDV9Ks8.Qf3YjMyQTNyQzNxojIwhXZiwiM4kDN6ICZpJye.9JiN1IzUIJiOicGbhJCLiQ1VKJiOiAXe0Jye',\n",
    "#     'machine_name': 'Baihua',\n",
    "#     'num_qubits': len(qubit_indices),\n",
    "#     'exp_name': f'{len(qubit_indices)}-qubit_{circuit_type}_qubits_{\"-\".join(map(str, qubit_indices))}'  # Include number of qubits, circuit type and qubit indices\n",
    "# }\n",
    "# shot_range = [int(1e3), 5*int(1e3), int(1e4), int(1e5)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "# repetition_code_receiver = ShotTestReceiver(shot_test_platform_config, shot_range=shot_range, circuit_nums=1)\n",
    "# repetition_code_receiver.submit_experiment(create_repetition_code_circuit(qubit_indices=qubit_indices, circuit_type=circuit_type))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# repetition_code_receiver.receive_results()\n",
    "# repetition_code_receiver.save_results(f'../results/{date}/repetition_code/')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
